package com.jcl.recruit.corp.logic;

import com.jcl.recruit.core.constants.Constants;
import com.jcl.core.utils.BaseUtils;
import com.jcl.core.utils.CollectionUtils;
import com.jcl.core.utils.DateUtils;
import com.jcl.recruit.corp.mapper.SubcompanyMapper;
import com.jcl.recruit.corp.model.Subcompany;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.*;

/**
 * @ClassName SubcompanyLogic
 * @Description TODO
 * @Author yyliu
 * @Date 2018/9/12 17:20
 * @Version 1.0
 **/
@Component
public class SubcompanyLogic implements SubcompanyMapper {

    @Autowired
    private SubcompanyMapper subcompanyMapper;

    public List<Map<String, Object>> contractTempCorpList(List<Subcompany> subcompanies){
        List<Map<String, Object>> dataList = new ArrayList<>();
        if(CollectionUtils.isNotEmpty(subcompanies)){
            for(Subcompany subcompany: subcompanies){
                Map<String, Object> corpMap = new HashMap<>();
                corpMap.put("fullname", subcompany.getFullname());
                corpMap.put("code", subcompany.getCode());
                dataList.add(corpMap);
            }
        }
        return dataList;
    }
    /* *
     * @Author yyliu
     * @Description 完善企业信息
     * @Date 18:23 2018/9/12
     * @Param [company]
     * @return com.example.model.Subcompany
     **/
    public Subcompany completeCorp(Subcompany company){

        company = initDefaultMsg(company);

        return company;
    }

    /* *
     * @Author yyliu
     * @Description 默认构造企业基本参数
     * @Date 18:23 2018/9/12
     * @Param [company]
     * @return com.example.model.Subcompany
     **/
    private Subcompany initDefaultMsg(Subcompany company){

        Map<String,Object> maxMap = subcompanyMapper.findMaxId();
        Integer maxId = 0;
        if(null != maxMap && null != maxMap.get("maxId")){
            maxId = Integer.parseInt(maxMap.get("maxId").toString());
        }
        maxId ++;
        String code = BaseUtils.madeCorpCode(maxId.toString());
        company.setCode(code);
        // 试用版
        company.setMealPackage(Constants.MealPackage.default_package);
        company.setAbateDate(DateUtils.addDays(new Date(), Constants.MealPackage.default_day));
        company.setAddDate(new Date());
        return company;
    }

    public Map<String, String> getCorpNameMap(List<String> corpCodes){
        Map<String, String> map = new HashMap<>();
        List<Subcompany> corpList = subcompanyMapper.findListByCorpCodes(corpCodes);
        if(CollectionUtils.isNotEmpty(corpList)){
            for(Subcompany subcompany: corpList){
                map.put(subcompany.getCode(), subcompany.getFullname());
            }
        }
        return map;
    }
    @Override
    public List<Subcompany> selectAllComanyInfosBySuperId(Integer superId) {
        return subcompanyMapper.selectAllComanyInfosBySuperId(superId);
    }

    @Override
    public List<Subcompany> findListByCorpCodes(List<String> corpCodes) {
        return subcompanyMapper.findListByCorpCodes(corpCodes);
    }

    @Override
    public Subcompany findOneByCode(String code) {
        return subcompanyMapper.findOneByCode(code);
    }

    @Override
    public Map<String,Object> findMaxId() {
        return subcompanyMapper.findMaxId();
    }

    @Override
    public List<Subcompany> findOneByEmail(String email, String name) {
        return subcompanyMapper.findOneByEmail(email, name);
    }

    @Override
    public Subcompany findOneById(Integer id) {
        return subcompanyMapper.findOneById(id);
    }

    @Override
    public int add(Subcompany company) {
        return subcompanyMapper.add(company);
    }

    @Override
    public int update(Subcompany company) {
        return subcompanyMapper.update(company);
    }
}
